package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IHomeService;
import jodd.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author dinghongrui
 * @create 2022/09/20 20:42
 */
@Service
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;


    @Override
    public Set<PanelDto> homepage() {
        return null;
    }

    @Override
    public List<PanelDto> getPanel() {
        List<Panel> panels = panelMapper.selectAll();

        if (CollectionUtils.isEmpty(panels)) {
            return null;
        }

        for (int i = 0; i < panels.size(); i++) {
            if (panels.get(i).getPosition() != 0) {
                panels.remove(panels.get(i));
            }
        }

        for (Panel panel : panels) {
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
            panel.setPanelContentItems(panelContentItems);
        }
        List<PanelDto> panelDtos = new ArrayList<>();

        for (Panel panel : panels) {
            PanelDto panelDto = contentConverter.panel2Dto(panel);
            panelDtos.add(panelDto);
        }

        return panelDtos;
    }

}
